package com.sanqi.common.mybatisplus;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sanqi.common.utils.SecurityUtils;

import cn.hutool.core.date.DateUtil;

@Component
public class SqMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前的用户名称
        String username = null;
        Long deptId = null;
        try {
            username = SecurityUtils.getUsername();
            deptId = SecurityUtils.getDeptId();
        } catch (Exception e) {
            
        }
        // 插入操作自动填充
        this.setFieldValByName("createTime", DateUtil.date(), metaObject);
        this.setFieldValByName("createBy", username, metaObject);
        this.setFieldValByName("sysOrgCode", null == deptId ? null : String.valueOf(deptId), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取当前的用户名称 
        String username = null;
        try {
            username = SecurityUtils.getUsername();
        } catch (Exception e) {
            
        }
        // 插入操作自动填充
        this.setFieldValByName("updateTime", DateUtil.date(), metaObject);
        this.setFieldValByName("updateBy", username, metaObject);
    }

}
